#include "bsp_usart1.h"

/* BSP-USART1 串口 */

/*
 * USART1_TX : PA9-AF7
 * USART1_RX : PA10-AF7
*/

#define USART_PORT USART1


/* static var */

static void (*usart1_irq_rx_handler)(int) = NULL;

/* static func */

/**
 * @brief usart1中断服务函数
 * 
 */
void USART1_IRQHandler(void)
{
    uint32_t reg_isr = USART_PORT->ISR;
    uint32_t reg_icr;
    int ch;
    static uint8_t usart1_irq_rx_cnt = 0;

    /* 接收中断 */
    if (reg_isr & USART_ISR_RXNE)
    {
        ch = (uint8_t)USART_PORT->RDR;
        usart1_irq_rx_cnt++;
        if (usart1_irq_rx_handler != NULL)
        {
            usart1_irq_rx_handler(ch);
            if (usart1_irq_rx_cnt >= 64)
            {
                usart1_irq_rx_handler(-1);
                usart1_irq_rx_cnt = 0;
            }
        }
    }

    /* 空闲中断 */
    if (reg_isr & USART_ISR_IDLE)
    {
        ch = -1;
        usart1_irq_rx_cnt = 0;
        if (usart1_irq_rx_handler != NULL)
        {
            usart1_irq_rx_handler(-1);
        }
    }

    /* 清除中断标志位 USART_ICR_TCCF  */
    reg_icr = UART_CLEAR_PEF | UART_CLEAR_FEF | UART_CLEAR_NEF | UART_CLEAR_OREF | \
                UART_CLEAR_IDLEF | UART_CLEAR_LBDF | UART_CLEAR_CTSF | \
                UART_CLEAR_CMF | UART_CLEAR_WUF | UART_CLEAR_TXFECF;
    SET_BIT(USART_PORT->ICR, reg_icr);
    
}



/* export func */

/**
 * @brief 设置串口1终端处理函数
 * 
 * @param func 回调函数
 */
void bsp_usart1_set_irq_rx_handler(void (*func)(int))
{
	usart1_irq_rx_handler = func;
}


/**
 * @brief 串口1发送1个字符(同步发送模式)
 * 
 * @param ch 发送字符
 * @return int 
 */
int bsp_usart1_put_char(int ch)
{
    while ((USART_PORT->ISR & USART_ISR_TC) == 0); //等待上次发送完成
    USART_PORT->TDR = (uint8_t)ch;
    return ch;
}

/**
 * @brief 串口1发送缓存
 * 
 * @param buff 缓存
 * @param size 缓存大小
 * @return int 
 */
int bsp_usart1_send_buff(uint8_t *buff, uint32_t size)
{
    int i;

    for (i = 0; i < size; i++)
    {
        while ((USART_PORT->ISR & USART_ISR_TC) == 0); //等待上次发送完成
        USART_PORT->TDR = (uint8_t)buff[i];
    }
    return size;
}


/**
 * @brief usart1串口初始化
 * 
 * @param bound 波特率
 */
void bsp_usart1_init(uint32_t bound)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    UART_HandleTypeDef UartHandle = {0};

    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_USART1_CLK_ENABLE();

    /* USART1_TX: PA9-AF7 */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1_RX: PA10-AF7 */
    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* 串口配置: 8位, 1停止位, 无奇偶校验, 无流控 */
    UartHandle.Instance = USART_PORT;
    UartHandle.Init.BaudRate = bound;
    UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    UartHandle.Init.StopBits = UART_STOPBITS_1;
    UartHandle.Init.Parity = UART_PARITY_NONE;
    UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    UartHandle.Init.Mode = UART_MODE_TX_RX;
    UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    // UartHandle.Init.Prescaler = UART_PRESCALER_DIV1;
    // UartHandle.Init.FIFOMode = UART_FIFOMODE_DISABLE;
    // UartHandle.Init.TXFIFOThreshold = UART_TXFIFO_THRESHOLD_1_8;
    // UartHandle.Init.RXFIFOThreshold = UART_RXFIFO_THRESHOLD_1_8;
    UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&UartHandle) != HAL_OK)
    {
        // Error_Handler(__FILE__, __LINE__);
    }

    /* 中断 */
    HAL_NVIC_SetPriority(USART1_IRQn, 11, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);

    // SET_BIT(USART_PORT->ICR, USART_ICR_TCCF);  /* 清除TC发送完成标志 */
    SET_BIT(USART_PORT->RQR, USART_RQR_RXFRQ); /* 清除RXNE接收标志 */
    // USART_CR1_PEIE | USART_CR1_RXNEIE
    SET_BIT(USART_PORT->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE | USART_CR1_IDLEIE); /* 使能PE,RX接收,IDLE空闲中断 */

}


